package org.come.xingpan;

import com.tool.btn.FormsOnOffBtn;
import com.tool.image.ImageMixDeal;
import com.tool.role.BaseXingpans;
import com.tool.role.RoleData;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
import org.come.bean.LoginResult;
import org.come.entity.Goodstable;
import org.come.until.CutButtonImage;
import org.come.until.GoodsListFromServerUntil;
import org.come.until.MessagrFlagUntil;
import org.come.until.UserMessUntil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

 

/**
 * 星魂主面板
 * <p>
 * Title : JpanelSoulBackMain
 * </p>
 *
 * @author : HGC
 * @version : 1.0.0
 * @date : 2019年8月7日 下午2:11:17
 */
public class JpanelXingBackMain extends JPanel {

    class SelectBox {
        private Integer x;
        private Integer y;
        private Integer index;
        private Boolean isSelect = false;
    }

    /**
     * cardLab星盘点击背景白<br>
     * labMeridians 星魂 36<br>
     * shuliang 星魂拥有数量<br>
     * biaoti 6个标题<br>
     * icon 激活蒙版<br>
     */
    private JLabel[] cardLab, labMeridians, shuliang, biaoti, huise, icon;

    /**
     * cardLab1 星盘点击-黄<br>
     * xinghun 拥有魂值<br>
     */
    private JLabel cardLab1, xinghun;
    /**
     * yesBtn确认按钮30<br>
     * noBtn取消按钮31<br>
     * 32 激活大招<br>
     * 33 兑换<br>
     */
    private BtnStarCard yesBtn, noBtn, noBtn1, noBtn2;

    private ArrayList<BigDecimal> starCardList;
    private int ChooseNum = -1;

    private SelectBox selectBox;

    private Graphics jGraphics;

    public JpanelXingBackMain() {
        setPreferredSize(new Dimension(800, 349));
        setLayout(null);
        setOpaque(false);

        // 关闭按钮事件
        FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 121);
        offBtn.setBounds(810 - 30, 10, 25, 25);
        this.add(offBtn);
        getBiaoti();//标题
        getShuliang();//数量
        getIcon();//蒙版
        getLabMeridians();//星魂

        getNoBtn();
        getYesBtn();
        getNoBtn1();
        getNoBtn2();
        cardLab1 = new JLabel();
        cardLab1.setIcon(CutButtonImage.getImage("inkImg/hongmu/2/21_png.xy2uiimg.atips.png", 50, 50));
        this.add(cardLab1);
        getCardLab(); //框框

        getXinghun();
        ChooseNum = -1;
        viewCultivateBtn();
    }


    private ImageIcon iconBack;

    static Sprite tcp = SpriteFactory.VloadSprite("inkImg/hongmu/1/1367-4D92C12D.tcp", null);//特效路径

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        if (jGraphics == null) {
            jGraphics = g;
        }

        if (iconBack == null) {
            iconBack = new ImageIcon("inkImg/old/background/S281.png");
        }
        g.drawImage(iconBack.getImage(), 0, 0, 800, 349, this);

        if (getChooseNum() != -1) {
            tcp.updateToTime(ImageMixDeal.userimg.getTime(), 0);
            tcp.draw(g, labMeridians[ChooseNum].getBounds().x + 22, labMeridians[ChooseNum].getBounds().y + 13);
        }

    }

    public ImageIcon getIconBack() {
        return iconBack;
    }

    public void setIconBack(ImageIcon iconBack) {
        this.iconBack = iconBack;
    }

    class MListener implements MouseListener {
        private int num;

        public MListener(int num) {
            super();
            this.num = num;

        }

        @Override
        public void mouseClicked(MouseEvent e) {


        }

        //mousePressed 松开鼠标执行
        @Override
        public void mousePressed(MouseEvent e) {
//            if (num >= RoleProperty.getRoleProperty().meridiansVector.size()) {
////                rolechange//X1
//                SendMessageUntil.toServer(Agreement.getAgreement().rolechangeAgreement("X1"+(num+1)));
//                return;
//            }
            ChooseNum = num;
//            if (ChooseNum != -1) {
//                cardLab1.setBounds(labMeridians[ChooseNum].getBounds().x + 25, labMeridians[ChooseNum].getBounds().y + 13, 50, 50);
//            }


        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE12);

        }

        @Override
        public void mouseExited(MouseEvent e) {
            MessagrFlagUntil.setMouse(MessagrFlagUntil.MOUSE1);

        }
    }

    public JLabel[] getLabMeridians() {
        if (labMeridians == null) {

            labMeridians = new JLabel[36];
            for (int i = 0; i < labMeridians.length; i++) {
                labMeridians[i] = new JLabel();
                // labMeridians[i].setOpaque(true);背景图
                // labMeridians[i].setBackground(Color.red); 背景颜色
                labMeridians[i].setIcon(CutButtonImage.getImage("skin/8100226/button/xp" + (i + 1) + ".png", 105, 105));
                labMeridians[i].addMouseListener(new MListener(i));
                this.add(labMeridians[i]);
            }
            labMeridians[0].setBounds(110, 20, 75, 70);
            shuliang[0].setBounds(140, 20, 50, 50);


            labMeridians[1].setBounds(160, 20, 75, 70);
            shuliang[1].setBounds(190, 20, 50, 50);

            labMeridians[2].setBounds(210, 20, 75, 70);
            shuliang[2].setBounds(240, 20, 50, 50);

            labMeridians[3].setBounds(110, 70, 75, 70);
            shuliang[3].setBounds(140, 70, 50, 50);

            labMeridians[4].setBounds(160, 70, 75, 70);
            shuliang[4].setBounds(190, 70, 50, 50);

            labMeridians[5].setBounds(210, 70, 75, 70);
            shuliang[5].setBounds(240, 70, 50, 50);

            labMeridians[6].setBounds(300, 20, 75, 70);
            shuliang[6].setBounds(330, 20, 50, 50);

            labMeridians[7].setBounds(350, 20, 75, 70);
            shuliang[7].setBounds(380, 20, 50, 50);

            labMeridians[8].setBounds(400, 20, 75, 70);
            shuliang[8].setBounds(430, 20, 50, 50);

            labMeridians[9].setBounds(300, 70, 75, 70);
            shuliang[9].setBounds(330, 70, 50, 50);

            labMeridians[10].setBounds(350, 70, 75, 70);
            shuliang[10].setBounds(380, 70, 50, 50);

            labMeridians[11].setBounds(400, 70, 75, 70);
            shuliang[11].setBounds(430, 70, 50, 50);

            labMeridians[12].setBounds(490, 20, 75, 70);
            shuliang[12].setBounds(520, 20, 50, 50);

            labMeridians[13].setBounds(540, 20, 75, 70);
            shuliang[13].setBounds(570, 20, 50, 50);

            labMeridians[14].setBounds(590, 20, 75, 70);
            shuliang[14].setBounds(620, 20, 50, 50);

            labMeridians[15].setBounds(490, 70, 75, 70);
            shuliang[15].setBounds(520, 70, 50, 50);

            labMeridians[16].setBounds(540, 70, 75, 70);
            shuliang[16].setBounds(570, 70, 50, 50);

            labMeridians[17].setBounds(590, 70, 75, 70);
            shuliang[17].setBounds(620, 70, 50, 50);

            labMeridians[18].setBounds(110, 170, 75, 70);
            shuliang[18].setBounds(140, 170, 50, 50);

            labMeridians[19].setBounds(160, 170, 75, 70);
            shuliang[19].setBounds(190, 170, 50, 50);

            labMeridians[20].setBounds(210, 170, 75, 70);
            shuliang[20].setBounds(240, 170, 50, 50);

            labMeridians[21].setBounds(110, 220, 75, 70);
            shuliang[21].setBounds(140, 220, 50, 50);

            labMeridians[22].setBounds(160, 220, 75, 70);
            shuliang[22].setBounds(190, 220, 50, 50);

            labMeridians[23].setBounds(210, 220, 75, 70);
            shuliang[23].setBounds(240, 220, 50, 50);

            labMeridians[24].setBounds(300, 170, 75, 70);
            shuliang[24].setBounds(330, 170, 50, 50);

            labMeridians[25].setBounds(350, 170, 75, 70);
            shuliang[25].setBounds(380, 170, 50, 50);

            labMeridians[26].setBounds(400, 170, 75, 70);
            shuliang[26].setBounds(430, 170, 50, 50);

            labMeridians[27].setBounds(300, 220, 75, 70);
            shuliang[27].setBounds(330, 220, 50, 50);

            labMeridians[28].setBounds(350, 220, 75, 70);
            shuliang[28].setBounds(380, 220, 50, 50);

            labMeridians[29].setBounds(400, 220, 75, 70);
            shuliang[29].setBounds(430, 220, 50, 50);

            labMeridians[30].setBounds(490, 170, 75, 70);
            shuliang[30].setBounds(520, 170, 50, 50);

            labMeridians[31].setBounds(540, 170, 75, 70);
            shuliang[31].setBounds(570, 170, 50, 50);

            labMeridians[32].setBounds(590, 170, 75, 70);
            shuliang[32].setBounds(620, 170, 50, 50);

            labMeridians[33].setBounds(490, 220, 75, 70);
            shuliang[33].setBounds(520, 220, 50, 50);

            labMeridians[34].setBounds(540, 220, 75, 70);
            shuliang[34].setBounds(570, 220, 50, 50);

            labMeridians[35].setBounds(590, 220, 75, 70);
            shuliang[35].setBounds(620, 220, 50, 50);


            for (int i = 0; i < labMeridians.length; i++) {
                icon[i].setBounds(labMeridians[i].getBounds().x + 29, labMeridians[i].getBounds().y + 2, 70, 70);
            }

        }
        return labMeridians;
    }

    //
    public JLabel[] getCardLab() {
        if (cardLab == null) {
            cardLab = new JLabel[36];
            for (int i = 0; i < cardLab.length; i++) {
                cardLab[i] = new JLabel();
                cardLab[i].setBounds(labMeridians[i].getBounds().x + 25, labMeridians[i].getBounds().y + 13, 50, 50);
                // cardLab[i].setOpaque(false);
                // cardLab[i].addMouseListener(new MouseListenerSoulBack(i, this));
                cardLab[i].setIcon(CutButtonImage.getImage("inkImg/hongmu/2/xpdj.png", 50, 50));
                this.add(cardLab[i]);
            }
        }
        return cardLab;
    }

    public JLabel[] getShuliang() {
        if (shuliang == null) {
            shuliang = new JLabel[36];
            for (int i = 0; i < shuliang.length; i++) {
                Goodstable goodstable = UserMessUntil.getgoodstable(new BigDecimal(51000 + i));
                int goodNum = GoodsListFromServerUntil.getGoodNum(goodstable.getGoodsid());
                shuliang[i] = new JLabel();
                shuliang[i].setFont(new Font("宋体", Font.BOLD, 15));
                shuliang[i].setForeground(Color.red);
                //shuliang[i].setBounds(labMeridians[i].getBounds().x+25, labMeridians[i].getBounds().y+13, 50, 50);
                shuliang[i].setText("" + goodNum);
                this.add(shuliang[i]);
            }
        }
        return shuliang;
    }

    //判断背包数量
    public JLabel[] getBiaoti() {
        if (biaoti == null) {
            biaoti = new JLabel[6];
            for (int i = 0; i < biaoti.length; i++) {
                biaoti[i] = new JLabel();
                biaoti[i].setFont(new Font("宋体", Font.PLAIN, 20));
                biaoti[i].setForeground(Color.yellow);
                if (i < 3) {
                    biaoti[i].setBounds(180 + 190 * i, 8, 200, 30);
                } else {
                    biaoti[i].setBounds(180 + 190 * (i - 3), 158, 200, 30);
                }
                biaoti[i].setText("等级" + (i + 1));
                this.add(biaoti[i]);
            }
        }
        return biaoti;
    }

    public void setCardLab(JLabel[] cardLab) {
        this.cardLab = cardLab;
    }

    public BtnStarCard getYesBtn() {
        if (yesBtn == null) {
            yesBtn = new BtnStarCard("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "激活", 30,
                    this);
            yesBtn.setBounds(285, 310, 59, 24);
            this.add(yesBtn);
        }
        return yesBtn;
    }

    public void setYesBtn(BtnStarCard yesBtn) {
        this.yesBtn = yesBtn;
    }

    public BtnStarCard getNoBtn() {
        if (noBtn == null) {
            noBtn = new BtnStarCard("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "分解", 31,
                    this);
            noBtn.setBounds(500, 310, 59, 24);
            this.add(noBtn);
        }
        return noBtn;
    }

    public void setNoBtn(BtnStarCard noBtn) {
        this.noBtn = noBtn;
    }

    public BtnStarCard getNoBtn1() {
        if (noBtn1 == null) {
            noBtn1 = new BtnStarCard("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "大招", 32,
                    this);
            noBtn1.setBounds(585, 310, 59, 24);
            this.add(noBtn1);
        }
        return noBtn1;
    }

    public void setNoBtn1(BtnStarCard noBtn1) {
        this.noBtn1 = noBtn1;
    }

    public BtnStarCard getNoBtn2() {
        if (noBtn2 == null) {
            noBtn2 = new BtnStarCard("skin/8100226/newbutton/B20.png", 1, UIUtils.COLOR_WHITE2, UIUtils.TEXT_HY16, "兑换", 33,
                    this);
            noBtn2.setBounds(200, 310, 59, 24);
            this.add(noBtn2);
        }
        return noBtn2;
    }

    public void setNoBtn2(BtnStarCard noBtn2) {
        this.noBtn2 = noBtn2;
    }

    public JLabel getXinghun() {
        if (xinghun == null) {
            xinghun = new JLabel();
            xinghun.setFont(new Font("宋体", Font.BOLD, 16));
            xinghun.setForeground(Color.yellow);
            xinghun.setBounds(180 + 190, 305, 150, 30);
            BigDecimal big = RoleData.getRoleData().getLoginResult().getScoretype("魂值") != null ? RoleData
                    .getRoleData().getLoginResult().getScoretype("魂值")
                    : new BigDecimal(0);
            xinghun.setText("拥有魂值:" + big.toString());
            this.add(xinghun);
        }
        return xinghun;
    }

    //页面刷新
    public void viewCultivateBtn() {
        for (int i = 0; i < shuliang.length; i++) {
            Goodstable goodstable = UserMessUntil.getgoodstable(new BigDecimal(51000 + i));
            int goodNum = GoodsListFromServerUntil.getGoodNum(goodstable.getGoodsid());
            shuliang[i].setText("" + goodNum);
        }
        BigDecimal big = RoleData.getRoleData().getLoginResult().getScoretype("魂值") != null ? RoleData
                .getRoleData().getLoginResult().getScoretype("魂值")
                : new BigDecimal(0);
        xinghun.setText("拥有魂值:" + big.toString());

        LoginResult loginResult = RoleData.getRoleData().getLoginResult();
        if (loginResult.getXingpans() != null) {
            String[] vv = loginResult.getXingpans().split("\\|");
            for (int i = 0; i < vv.length; i++) {
                String[] vvv = vv[i].split("_");
                String[] vvvv = vvv[1].split("=");
                for (int u = 0; u < vvvv.length; u++) {
                    icon[Integer.valueOf(vvvv[u]) - 1].setIcon(null);
                }
            }
        }

    }

    //页面刷新
    public void viewCultivateBtn1(BaseXingpans baseXingpans) {
        for (int i = 0; i < shuliang.length; i++) {
            Goodstable goodstable = UserMessUntil.getgoodstable(new BigDecimal(51000 + i));
            int goodNum = GoodsListFromServerUntil.getGoodNum(goodstable.getGoodsid());
            shuliang[i].setText("" + goodNum);
        }
        BigDecimal big = RoleData.getRoleData().getLoginResult().getScoretype("魂值") != null ? RoleData
                .getRoleData().getLoginResult().getScoretype("魂值")
                : new BigDecimal(0);
        xinghun.setText("拥有魂值:" + big.toString());

        String vvvv = unicodeStr2String(baseXingpans.getExp());
        String[] vvv = vvvv.split("=");
        for (int i = 0; i < vvv.length; i++) {
            icon[Integer.valueOf(vvv[i]) - 1].setIcon(null);
        }


    }

    public ArrayList<BigDecimal> getStarCardList() {
        if (starCardList == null) {
            starCardList = new ArrayList<>();
        }
        return starCardList;
    }

    public void setStarCardList(ArrayList<BigDecimal> starCardList) {
        this.starCardList = starCardList;
    }

    public JLabel[] getIcon() {
        if (icon == null) {
            icon = new JLabel[36];
            for (int i = 0; i < icon.length; i++) {
                icon[i] = new JLabel();

                icon[i].setIcon(CutButtonImage.getImage("inkImg/old/1/蒙版.png", 43, 44));
                this.add(icon[i]);
            }
            //判断星盘  是否激活
            LoginResult loginResult = RoleData.getRoleData().getLoginResult();
            if (loginResult.getXingpans() != null) {
                String[] vv = loginResult.getXingpans().split("\\|");
                for (int i = 0; i < vv.length; i++) {
                    String[] vvv = vv[i].split("_");
                    String[] vvvv = vvv[1].split("=");
                    for (int u = 0; u < vvvv.length; u++) {
                        icon[Integer.valueOf(vvvv[u]) - 1].setIcon(null);
                    }
                }
            }
        }
        return icon;
    }

    public void setIcon(JLabel[] icon) {
        this.icon = icon;
    }

    public int getChooseNum() {
        return ChooseNum;
    }

    public void setChooseNum(int chooseNum) {
        ChooseNum = chooseNum;
    }

    //解码
    public static String unicode2String(String unicode) {
        StringBuffer string = new StringBuffer();
        String[] hex = unicode.split("\\\\u");

        for (int i = 1; i < hex.length; i++) {
            // 转换出每一个代码点
            int data = Integer.parseInt(hex[i], 16);
            // 追加成string
            string.append((char) data);
        }

        return string.toString();
    }

    public static String unicodeStr2String(String unicodeStr) {
        int length = unicodeStr.length();
        int count = 0;
        /*正则匹配条件，可匹配“\\u”1到4位，一般是4位可直接使用 String regex = "\\\\u[a-f0-9A-F]{4}";*/
        String regex = "\\\\u[a-f0-9A-F]{1,4}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(unicodeStr);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            String oldChar = matcher.group();//原本的Unicode字符
            String newChar = unicode2String(oldChar);//转换为普通字符
            // int index = unicodeStr.indexOf(oldChar);
            // 在遇见重复出现的unicode代码的时候会造成从源字符串获取非unicode编码字符的时候截取索引越界等
            int index = matcher.start();

            sb.append(unicodeStr.substring(count, index));//添加前面不是unicode的字符
            sb.append(newChar);//添加转换后的字符
            count = index + oldChar.length();//统计下标移动的位置
        }
        sb.append(unicodeStr.substring(count, length));//添加末尾不是Unicode的字符
        return sb.toString();
    }
}
